package io.sqrtqiezi.spark.lagou

import org.apache.spark.sql.SparkSession

import scala.math.random

object SparkPi {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      .master("local[*]")
      .appName(this.getClass.getCanonicalName.init)
      .getOrCreate()

    val slices = 100
    val N = 100000000

    val n = spark.sparkContext.makeRDD(1 to N, slices)
      .map(_ => {
        val (x, y) = (random, random)
        if (x * x + y * y <= 1) 1 else 0
      }).sum()

    val pi = 4.0 * n / N
    println(s"pi = $pi")
  }
}
